/**
 * 
 */
package recognition.engine.nn;

import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;

import org.encog.ml.data.MLData;
import org.encog.ml.data.basic.BasicMLData;
import org.encog.neural.networks.BasicNetwork;
import org.encog.persist.EncogDirectoryPersistence;

import recognition.engine.Recognizer;
import recognition.input.CellInput;
import recognition.input.Input;
import recognition.output.PredictedCharacter;

/**
 * @author Louis
 *
 */
public class NNRecognizer extends Recognizer {
	
	final private BasicNetwork network;
	final private String[] labels = new String[]{"9","8","7","6","5","4","3","2","1","0"};
	/**
	 * @param strength
	 */
	public NNRecognizer(double strength) {
		super(strength);
		network = (BasicNetwork)EncogDirectoryPersistence.loadObject(new File("./outputs/NN/nn.trained"));
	}

	/* (non-Javadoc)
	 * @see recognition.engine.Recognizer#process(java.util.Hashtable)
	 */
	@Override
	public ArrayList<PredictedCharacter> process(
			Hashtable<Class<? extends Input<?>>, Input<?>> inputs) {
		
		
		MLData nnInput = new BasicMLData((double[])inputs.get(CellInput.class).getInput()) ;
		
		MLData nnOutput = network.compute(nnInput);
		
		ArrayList<PredictedCharacter> results = new ArrayList<PredictedCharacter>();
		
		final double[] output = nnOutput.getData();
		
		for(int o = 0; o<output.length; o++)
			results.add(new PredictedCharacter(labels[o], output[o]));
		
		return results;
	}

}
